/**
 * Created by yanwe on 2016/7/4.
 */
/**
 * Created by yanwe on 2016/6/26.
 */
(function(){
    'use strict';
    app.controller('RequisitionAuditListController',[
        '$scope','i18nService','requisitionAuditService','$state','$stateParams',function($scope,i18nService,requisitionAuditService,$state,$stateParams){
            $(".datetimepicker_").datetimepicker({
                format: 'yyyy-mm-dd hh:ii:ss',
                language:"cn",
                weekStart: 1,
                todayBtn:  "linked",
                startView: 2,
                autoclose: true,
                todayHighlight: true,
                endDate : new Date()
            });
            $scope.filterOptions = {
                condition:{
                    userName: "",
                    loginName:"",
                    startTime:"",
                    endTime:"",
                    secUserId:""
                },
                useExternalFilter: true
            };
            i18nService.setCurrentLang('zh-cn');
            $scope.totalServerItems = 0;
            console.log($stateParams.pageSize,$stateParams.currentPage);
            $scope.pagingOptions = {
                pageSizes: [250, 500, 1000],
                pageSize: 250,
                currentPage: 1
            };
            $scope.setPagingData = function (data, page, pageSize) {
                $scope.myData = data.data;
                $scope.totalServerItems = data.totalServerItems;
                if (!$scope.$$phase) {
                    $scope.$apply();
                }
            };
            $scope.getPagedDataAsync = function (pageSize, page, condition) {
                setTimeout(function () {
                    if (condition) {
                        condition.currentPage=page;
                        condition.state="1";
                        requisitionAuditService.getRequisitionList(condition).success(function (largeLoad) {
                            $scope.setPagingData(largeLoad, page, pageSize);
                        });
                    } else {
                        condition={
                            currentPage:page,
                            state:"1"
                        };
                        requisitionAuditService.getRequisitionList(condition).success(function (largeLoad) {
                            $scope.setPagingData(largeLoad, page, pageSize);
                        });
                    }
                }, 100);
            };
            $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
            $scope.$watch('pagingOptions', function (newVal, oldVal) {
                if (newVal !== oldVal && newVal.currentPage !== oldVal.currentPage) {
                    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.condition);
                }
            }, true);
            $scope.$watch('filterOptions', function (newVal, oldVal) {
                if (newVal !== oldVal) {
                    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.condition);
                }
            }, true);
            $scope.showMe = function ($this) {
                $state.go('app.requisition_detail',{pkId:$this.row.entity.id});
                //console.log($this.row);
            };
            $scope.gridOptions = {
                paginationPageSizes: [15, 20, 25],
                paginationPageSize: 15,
                enableColumnResizing: true,
                enableGridMenu: true,
                columnDefs: [
                    {name: 'secUserId', pinnedLeft: true, displayName: '用户编号', width: 100},
                    {name: 'userName', pinnedLeft: true, displayName: '用户名称', width: 100},
                    {name: 'loginName', displayName: '登录名称', width: 180},
                    {name: 'nickName', displayName: '昵称', width: 100},
                    {name: 'realName', displayName: '真实名称', width: 100},
                    {name: 'userTypeName', displayName: '用户类型', width: 100},
                    {name: 'cellPhone', displayName: '电话号码', width: 100},
                    {name: 'createTime', displayName: '创建时间', width: 190},
                    {
                        name: '详情', width: 110,
                        pinnedRight: true,
                        cellTemplate: '<button class="btn btn-mini btn-primary table-btn" ng-click="grid.appScope.showMe(this)">详情</button>'
                    }
                ],
                data: 'myData'
            };
            $scope.searchByFilter=function(){
                $scope.filterOptions.condition=$scope.condition;
                console.log($scope.filterOptions);
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.condition);
            };
        }
    ]);

    app.controller('RequisitionDetailController',[
        '$scope','$stateParams','$state','requisitionAuditService',function($scope, $stateParams,$state,requisitionAuditService){
            $scope.pkId=$stateParams.pkId;
            requisitionAuditService.getRequisitionDetail($scope.pkId).then(function(success){
                $scope.requisitionDetail=success.data;
                $('#resumeSummernote').summernote({
                    height: 400,
                    minHeight: 300,
                    maxHeight: 500,
                    focus: true,
                    lang:'zh-CN'
                });
                $('#experienceSummernote').summernote({
                    height: 400,
                    minHeight: 300,
                    maxHeight: 500,
                    focus: true,
                    lang:'zh-CN'
                });
                $('#brandSummernote').summernote({
                    height: 400,
                    minHeight: 300,
                    maxHeight: 500,
                    focus: true,
                    lang:'zh-CN',
                });
                if($scope.requisitionDetail.userType=="1"){
                    $("#designerInfo").show();
                    $("#stylistInfo").hide();
                    $("#chargeOfStoreInfo").hide();
                    $('#brandSummernote').summernote("code",$scope.requisitionDetail.brandIntroduction);
                }else if($scope.requisitionDetail.userType=="2"){
                    $("#designerInfo").hide();
                    $("#stylistInfo").show();
                    $("#chargeOfStoreInfo").hide();
                    $('#resumeSummernote').summernote("code",$scope.requisitionDetail.resume);
                    $('#experienceSummernote').summernote("code",$scope.requisitionDetail.experience);
                }else{
                    $("#designerInfo").hide();
                    $("#stylistInfo").hide();
                    $("#chargeOfStoreInfo").show();
                }
            });
            $scope.backToRequisitionList=function(){
                $state.go('app.requisition_audit_list');
            };
            $scope.audit=function(){
                $state.go('app.requisition_audit',{pkId:$scope.pkId});
            }
        }
    ]);

    app.controller('RequisitionAuditController',[
        '$scope','$stateParams','$state','requisitionAuditService',function($scope, $stateParams,$state,requisitionAuditService){
            $scope.pkId=$stateParams.pkId;
            $scope.message="";
            $scope.auditPassed=function(){
                requisitionAuditService.audit($scope.pkId,true,$scope.message).success(function () {
                    console.log("审核通过");
                    $state.go('app.requisition_detail',{pkId:$scope.pkId});
                });
            };
            $scope.auditFailed=function(){
                requisitionAuditService.audit($scope.pkId,false,$scope.message).success(function () {
                    console.log("审核不通过");
                    $state.go('app.requisition_detail',{pkId:$scope.pkId});
                });
            };
            $scope.backToDetail=function(){
                $state.go('app.requisition_detail',{pkId:$scope.pkId});
            }
        }
    ]);
})();
